﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Windows;
using Mal.Balades.Admin.AdminWebService;


namespace Mal.Balades.Admin.Services
{
    public class AdminService : IAdminService
    {
        #region Fields
        AdminWebServiceSoapClient service;
        #endregion

        #region Construction / Destruction
        public AdminService()
        {
            string url = Application.Current.Host.Source.AbsoluteUri.Replace(Application.Current.Host.Source.AbsolutePath, "");
            url += "/Administration/AdminWebService.asmx";
            Uri uri = new Uri(url, UriKind.Absolute);
            service = new AdminWebServiceSoapClient();
            EndpointAddress endPointAddress = new EndpointAddress(uri);
            service.Endpoint.Address = endPointAddress;

            service.GetVisitCompleted += new EventHandler<GetVisitCompletedEventArgs>(this.OnGetVisitCompleted);
            service.GetVisitInformationsCompleted += new EventHandler<GetVisitInformationsCompletedEventArgs>(this.OnGetVisitInformationsCompleted);
            service.GetVisitPicturesCompleted += new EventHandler<GetVisitPicturesCompletedEventArgs>(this.OnGetVisitPicturesCompleted);
            service.GetVisitsForDashboardCompleted += new EventHandler<GetVisitsForDashboardCompletedEventArgs>(this.OnGetVisitsForDashboardCompleted);
            service.SaveVisitCompleted += new EventHandler<SaveVisitCompletedEventArgs>(this.OnSaveVisitCompleted);
            service.GetCategoriesCompleted += new EventHandler<GetCategoriesCompletedEventArgs>(this.OnGetCategoriesCompleted);
            service.GetInformationTypesCompleted += new EventHandler<GetInformationTypesCompletedEventArgs>(this.OnGetInformationTypesCompleted);
            service.GetPicturesTypesCompleted += new EventHandler<GetPicturesTypesCompletedEventArgs>(this.OnGetPicturesTypesCompleted);
            service.GetFeedsCompleted += new EventHandler<GetFeedsCompletedEventArgs>(this.OnGetFeedsCompleted);
            service.UpdateFeedItemsCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(this.OnUpdateFeedItemsCompleted);

        }
        ~AdminService()
        {
            Cleanup();
        }
        #endregion

        #region Properties

        #endregion

        #region Methods
        public void Cleanup()
        {
            if (service != null)
            {
                service.GetVisitCompleted -= new EventHandler<GetVisitCompletedEventArgs>(this.OnGetVisitCompleted);
                service.GetVisitInformationsCompleted -= new EventHandler<GetVisitInformationsCompletedEventArgs>(this.OnGetVisitInformationsCompleted);
                service.GetVisitPicturesCompleted -= new EventHandler<GetVisitPicturesCompletedEventArgs>(this.OnGetVisitPicturesCompleted);
                service.GetVisitsForDashboardCompleted -= new EventHandler<GetVisitsForDashboardCompletedEventArgs>(this.OnGetVisitsForDashboardCompleted);
                service.SaveVisitCompleted -= new EventHandler<SaveVisitCompletedEventArgs>(this.OnSaveVisitCompleted);
                service.GetCategoriesCompleted -= new EventHandler<GetCategoriesCompletedEventArgs>(this.OnGetCategoriesCompleted);
                service.GetInformationTypesCompleted -= new EventHandler<GetInformationTypesCompletedEventArgs>(this.OnGetInformationTypesCompleted);
                service.GetPicturesTypesCompleted -= new EventHandler<GetPicturesTypesCompletedEventArgs>(this.OnGetPicturesTypesCompleted);
                service.GetFeedsCompleted -= new EventHandler<GetFeedsCompletedEventArgs>(this.OnGetFeedsCompleted);
                service.UpdateFeedItemsCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(this.OnUpdateFeedItemsCompleted);
                service.InnerChannel.Close();
                service = null;
            }
        }
        #endregion

        #region Events
        private void OnSaveVisitCompleted(object sender, SaveVisitCompletedEventArgs e)
        {
            if (this.SaveVisitCompleted != null)
            {
                ServiceResponseArgs<ISaveVisitResult> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<ISaveVisitResult>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<ISaveVisitResult>(e.Result);
                }
                this.SaveVisitCompleted(result);
            }
        }

        private void OnGetVisitsForDashboardCompleted(object sender, GetVisitsForDashboardCompletedEventArgs e)
        {
            if (this.GetVisitsForDashboardCompleted != null)
            {
                ServiceResponseArgs<ReadOnlyCollection<IVisitDashboard>> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<ReadOnlyCollection<IVisitDashboard>>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<ReadOnlyCollection<IVisitDashboard>>(VisitDashboard.Convert(e.Result));
                }
                this.GetVisitsForDashboardCompleted(result);
            }
        }

        private void OnGetVisitPicturesCompleted(object sender, GetVisitPicturesCompletedEventArgs e)
        {
            if (this.GetVisitPicturesCompleted != null)
            {
                ServiceResponseArgs<ReadOnlyCollection<IPicture>> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<ReadOnlyCollection<IPicture>>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<ReadOnlyCollection<IPicture>>(Picture.Convert(e.Result));
                }
                this.GetVisitPicturesCompleted(result);
            }
        }

        private void OnGetVisitInformationsCompleted(object sender, GetVisitInformationsCompletedEventArgs e)
        {
            if (this.GetVisitInformationsCompleted != null)
            {
                ServiceResponseArgs<ReadOnlyCollection<IInformation>> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<ReadOnlyCollection<IInformation>>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<ReadOnlyCollection<IInformation>>(Information.Convert(e.Result));
                }
                this.GetVisitInformationsCompleted(result);
            }
        }

        private void OnGetVisitCompleted(object sender, GetVisitCompletedEventArgs e)
        {
            if (this.GetVisitCompleted != null)
            {
                ServiceResponseArgs<IVisitAdmin> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<IVisitAdmin>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<IVisitAdmin>(e.Result);
                }
                this.GetVisitCompleted(result);
            }
        }

        private void OnGetPicturesTypesCompleted(object sender, GetPicturesTypesCompletedEventArgs e)
        {
            if (this.GetPictureTypesCompleted != null)
            {
                ServiceResponseArgs<List<IPictureType>> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<List<IPictureType>>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<List<IPictureType>>(PictureType.Convert(e.Result));
                }
                this.GetPictureTypesCompleted(result);
            }
        }

        private void OnGetInformationTypesCompleted(object sender, GetInformationTypesCompletedEventArgs e)
        {
            if (this.GetInformationTypesCompleted != null)
            {
                ServiceResponseArgs<List<IInformationType>> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<List<IInformationType>>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<List<IInformationType>>(InformationType.Convert(e.Result));
                }
                this.GetInformationTypesCompleted(result);
            }
        }

        private void OnGetCategoriesCompleted(object sender, GetCategoriesCompletedEventArgs e)
        {
            if (this.GetCategoriesCompleted != null)
            {
                ServiceResponseArgs<List<ICategory>> result;
                if (e.Error != null)
                {
                    result = new ServiceResponseArgs<List<ICategory>>(e.Error, e.UserState);
                }
                else
                {
                    result = new ServiceResponseArgs<List<ICategory>>(Category.Convert(e.Result));
                }
                this.GetCategoriesCompleted(result);
            }
        }

        private void OnUpdateFeedItemsCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            ServiceResponseArgs<bool> result;
            if (e.Error != null)
            {
                result = new ServiceResponseArgs<bool>(e.Error, e.UserState);
            }
            else
            {
                result = new ServiceResponseArgs<bool>(true, e.UserState);
            }
            this.UpdateFeedItemsCompleted(result);
        }

        private void OnGetFeedsCompleted(object sender, GetFeedsCompletedEventArgs e)
        {
            ServiceResponseArgs<ReadOnlyCollection<IFeed>> result;
            if (e.Error != null)
            {
                result = new ServiceResponseArgs<ReadOnlyCollection<IFeed>>(e.Error, e.UserState);
            }
            else
            {
                result = new ServiceResponseArgs<ReadOnlyCollection<IFeed>>(FeedAdmin.Convert(e.Result));
            }
            this.GetFeedsCompleted(result);
        }
        #endregion

        #region IAdminService Members

        public event ServiceResponse<ReadOnlyCollection<IVisitDashboard>> GetVisitsForDashboardCompleted;

        public void GetVisitsForDashboardAsync()
        {
            GetVisitsForDashboardAsync(null);
        }

        public void GetVisitsForDashboardAsync(object userState)
        {
            service.GetVisitsForDashboardAsync(userState);
        }


        public event ServiceResponse<IVisitAdmin> GetVisitCompleted;
        public void GetVisitAsync(int visitId)
        {
            GetVisitAsync(visitId, null);
        }

        public void GetVisitAsync(int visitId, object userState)
        {
            service.GetVisitAsync(visitId, userState);
        }


        public event ServiceResponse<System.Collections.ObjectModel.ReadOnlyCollection<IPicture>> GetVisitPicturesCompleted;
        public void GetVisitPicturesAsync(int visitId)
        {
            GetVisitPicturesAsync(visitId, null);
        }

        public void GetVisitPicturesAsync(int visitId, object userState)
        {
            service.GetVisitPicturesAsync(visitId, userState);
        }


        public event ServiceResponse<System.Collections.ObjectModel.ReadOnlyCollection<IInformation>> GetVisitInformationsCompleted;
        public void GetVisitInformationsAsync(int visitId)
        {
            GetVisitInformationsAsync(visitId, null);
        }

        public void GetVisitInformationsAsync(int visitId, object userState)
        {
            service.GetVisitInformationsAsync(visitId, userState);
        }


        public event ServiceResponse<ISaveVisitResult> SaveVisitCompleted;
        public void SaveVisitAsync(ISaveVisitObject visitToSave)
        {
            SaveVisitAsync(visitToSave, null);
        }

        public void SaveVisitAsync(ISaveVisitObject visitToSave, object userState)
        {
            SaveVisitObject saveVisitObject = SaveVisitObject.Convert(visitToSave);
            service.SaveVisitAsync(saveVisitObject, userState);
        }

        public void GetCategoriesAsync()
        {
            service.GetCategoriesAsync();
        }

        public void GetCategoriesAsync(object userState)
        {
            service.GetCategoriesAsync(userState);
        }

        public event ServiceResponse<List<ICategory>> GetCategoriesCompleted;

        public void GetInformationTypesAsync()
        {
            service.GetInformationTypesAsync();
        }

        public void GetInformationTypesAsync(object userState)
        {
            service.GetInformationTypesAsync(userState);
        }

        public event ServiceResponse<List<IInformationType>> GetInformationTypesCompleted;

        public void GetPictureTypesAsync()
        {
            service.GetPicturesTypesAsync();
        }

        public void GetPictureTypesAsync(object userState)
        {
            service.GetPicturesTypesAsync(userState);
        }

        public event ServiceResponse<List<IPictureType>> GetPictureTypesCompleted;

        public void GetFeedsAsync()
        {
            service.GetFeedsAsync();
        }

        public void GetFeedsAsync(object userState)
        {
            service.GetFeedsAsync(userState);
        }

        public event ServiceResponse<ReadOnlyCollection<IFeed>> GetFeedsCompleted;

        public void UpdateFeedItemsAsync(int feedId)
        {
            service.UpdateFeedItemsAsync(feedId);
        }

        public void UpdateFeedItemsAsync(int feedId, object userState)
        {
            service.UpdateFeedItemsAsync(feedId, userState);
        }

        public event ServiceResponse<bool> UpdateFeedItemsCompleted;
        #endregion

    }
}
